﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ClientLibrary
{
    public class SystemState
    {
        public int CurrentBatch { get; set; }
        public int LastBatch { get; set; }
        public bool FirstRun { get; set; }
        public string Mysql_user { get; set; }
        public string Mysql_password { get; set; }
        public string Mysql_database { get; set; }
        public string Mysql_host { get; set; }
        public string Xml_path { get; set; }
        public string Ghostscript_path { get; set; }
        private string SettingsFile = "client_settings.xml";
        public string TemplatesFile = "templates.xml";
        private List<Template> Templates = new List<Template>();

        public SystemState()
        {            
            this.Initalize();
            if (this.FirstRun)
            {
                // Create export directory
                Directory.CreateDirectory(@"Export");
                // Create pdf, xml directories
                Directory.CreateDirectory(@"Export\PDF");
                Directory.CreateDirectory(@"Export\XML");
                Directory.CreateDirectory(@"TEMP_IMAGES");
            }
        }

        public List<Template> getTemplates()
        {
            return this.Templates;
        }

        public void Initalize()
        {
            XML xml = new XML(this.SettingsFile, "");

            // reading config file
            if (!File.Exists(this.SettingsFile))
            {
                xml.WriteXML_Settings("yes", "", "", "", "", "", "", "0");
            }
            xml.ReadXML_Settings(this);
            this.CurrentBatch = this.LastBatch + 1;

            // reading template file
            xml.xml_filename = this.TemplatesFile;
            if(!File.Exists(this.TemplatesFile))
            {
                xml.WriteXML_Templates(this);
            }
            xml.ReadXML_Templates(this);
        }

        public void SaveStateToXML()
        {
            XML xml = new XML(this.SettingsFile, "");            
            xml.WriteXML_Settings(this.isRun(), this.Mysql_user, this.Mysql_password, this.Mysql_database, this.Mysql_host, this.Xml_path, this.Ghostscript_path, this.LastBatch.ToString());
        }

        public void CreateTemplateXML()
        {
            XML xml = new XML(this.TemplatesFile, "");
            xml.WriteXML_Templates(this);
        }

        public string[] ReadPDF(string file)
        {
            string input = Path.GetFullPath(file);

            PDFtoImage pdf = new PDFtoImage(Path.GetFileNameWithoutExtension(file), this.Ghostscript_path);
            //copy pdf file and rename it.
            string newfile = pdf.CopyFilePDF(Path.GetDirectoryName(file), Path.GetFileName(file));
            //create jpg images 
            pdf.PdfToJpg(newfile, CurrentBatch);
            CurrentBatch += 1;
            LastBatch += 1;                        
            SaveStateToXML();
            // create file objects
            string[] files = Directory.GetFiles(@"TEMP_IMAGES"); // filenames
            return files;
        }

        public string[] getTempImages()
        {
            return Directory.GetFiles(@"TEMP_IMAGES");
        }

        public void DeleteTemporaryImageFiles()
        {
            string[] files = Directory.GetFiles(@"TEMP_IMAGES");
            for (int i = 0; i < files.Length; i++)
            {
                File.Delete(files[i]);
            }
        }

        private string isRun()
        {
            if (this.FirstRun)
            {
                return "yes";
            }
            else
            {
                return "no";
            }
        }
    }
}
